package com.ming.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel03 {
    public static void main(String[] args) throws IOException {

        FileInputStream fileInputStream = new FileInputStream("file01.txt");
        FileChannel fileChannelIn = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("1.txt");
        FileChannel fileChannelOut = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);

        while (true) {

            byteBuffer.clear();

            int read = fileChannelIn.read(byteBuffer);
            if (read == -1) {
                break;
            }
            byteBuffer.flip();
            //将buffer中的数据写到fileChannelOut
            fileChannelOut.write(byteBuffer);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}
